<?php

if (!isset($_SERVER['SHELL']) && (!isset($_SERVER['SESSIONNAME']) || $_SERVER['SESSIONNAME'] != 'Console')) exit;

$in[0] = file_get_contents("vklang_proc.php");
$in[1] = file_get_contents("init_parser_proc.php");

$out[0] = $in[0];
$out[1] = $in[1];

$quot = "(?:'|\")";

foreach ($in as $n => $file) {
  preg_match("#/\*<CLASS DEF (.*?) (.*?)>\*/#", $file, $match);
  $class_name = $match[1];
  $pref = $match[2];
  
  $out[$n] = str_replace(array($match[0], "/*</CLASS DEF>*/"), array("class {$class_name} {", "}"), $out[$n]);
  
  // CLASS CONST
  preg_match("#/\*<CLASS CONST>\*/(.*?)/\*</CLASS CONST>\*/#ms", $out[$n], $match1);
  $lines = explode("\n", $match1[1]);
  foreach ($lines as $line) {
    preg_match("#define\(\s*{$quot}(.*?)_CONST_(.*?){$quot}\s*,\s*(.*?)\s*\);#i", $line, $match);
    if ($match) {
      $out[$n] = str_replace($match[0], "const {$match[2]} = {$match[3]};", $out[$n]);
      foreach ($out as $m => $str) {
        $out[$m] = str_replace($match[1].'_CONST_'.$match[2], ($m == $n ? 'self::' : $class_name.'::').$match[2], $out[$m]);
      }
    }
  }
  $out[$n] = str_replace(array('/*<CLASS CONST>*/', '/*</CLASS CONST>*/'), array('', ''), $out[$n]);

  // CLASS VAR
  preg_match("#/\*<CLASS VAR>\*/(.*?)/\*</CLASS VAR>\*/#ms", $out[$n], $match1);
  if (isset($match1[1])) {
    $lines = explode("\n", $match1[1]);
    foreach ($lines as $line) {
      preg_match("#global\s*\\$(.*?)_var_(.*?)\s*;(?:.*?=\s*(.*?)\s*;)?\s*$#i", $line, $match);
      if ($match) {
        $out[$n] = str_replace($match[0], isset($match[3]) ? "var \${$match[2]} = {$match[3]};" : "var \${$match[2]};", $out[$n]);
        $out[$n] = str_replace('$'.$match[1].'_var_'.$match[2], '$this->'.$match[2], $out[$n]);
      }
    }
  }
  $out[$n] = str_replace(array('/*<CLASS VAR>*/', '/*</CLASS VAR>*/'), array('', ''), $out[$n]);
  
  // CLASS STATIC VAR
  preg_match("#/\*<CLASS STATIC VAR>\*/(.*?)/\*</CLASS STATIC VAR>\*/#ms", $out[$n], $match1);
  if (isset($match1[1])) {
    $lines = explode("\n", $match1[1]);
    foreach ($lines as $line) {
      preg_match("#global\s*\\$(.*?)_STATIC_(.*?)\s*;(?:.*?=\s*(.*?)\s*;)?\s*$#i", $line, $match);
      if ($match) {
        $out[$n] = str_replace($match[0], isset($match[3]) ? "static \${$match[2]} = {$match[3]};" : "static \${$match[2]};", $out[$n]);
        foreach ($out as $m => $str) {
          $out[$m] = str_replace('$'.$match[1].'_STATIC_'.$match[2], ($m == $n ? 'self::$' : $class_name.'::$').$match[2], $out[$m]);
        }
      }
    }
  }
  $out[$n] = str_replace(array('/*<CLASS STATIC VAR>*/', '/*</CLASS STATIC VAR>*/'), array('', ''), $out[$n]);
  
  // CLASS CONSTRUCTOR
  preg_match("#/\*<CLASS CONSTRUCTOR>\*/(.*?)/\*</CLASS CONSTRUCTOR>\*/#ms", $out[$n], $match1);
  if (isset($match1[1])) {
    $lines = explode("\n", $match1[1]);
    foreach ($lines as $line) {
      preg_match("#function (?:.*?)\(#i", $line, $match);
      if ($match) {
        $out[$n] = str_replace($match[0], "function __construct(", $out[$n]);
        break;
      }
    }
  }
  $out[$n] = str_replace(array('/*<CLASS CONSTRUCTOR>*/', '/*</CLASS CONSTRUCTOR>*/'), array('', ''), $out[$n]);

  // CLASS FUNC
  preg_match_all("#/\*<CLASS FUNC>\*/(.*?)/\*</CLASS FUNC>\*/#ms", $out[$n], $matches, PREG_SET_ORDER);
  foreach ($matches as $match1) {
    $lines = explode("\n", $match1[1]);
    foreach ($lines as $line) {
      preg_match("#function\s*(_?)(?:{$pref})_(.*?)\s*\(#i", $line, $match);
      if ($match) {
        $out[$n] = str_replace($match[0], ($match[1] == '_' ? "private function " : "function ").$match[2].'(', $out[$n]);
        $out[$n] = str_replace($match[1].$pref.'_'.$match[2].'(', '$this->'.$match[2].'(', $out[$n]);
        break;
      }
    }
  }
  $out[$n] = str_replace(array('/*<CLASS FUNC>*/', '/*</CLASS FUNC>*/'), array('', ''), $out[$n]);
  
  // CLASS STATIC FUNC
  preg_match_all("#/\*<CLASS STATIC FUNC>\*/(.*?)/\*</CLASS STATIC FUNC>\*/#ms", $out[$n], $matches, PREG_SET_ORDER);
  foreach ($matches as $match1) {
    $lines = explode("\n", $match1[1]);
    foreach ($lines as $line) {
      preg_match("#function\s*_(?:{$pref})_(.*?)\s*\(#i", $line, $match);
      if ($match) {
        $out[$n] = str_replace($match[0], "static function ".$match[1].'(', $out[$n]);
        foreach ($out as $m => $str) {
          $out[$m] = str_replace('_'.$pref.'_'.$match[1].'(', ($m == $n ? 'self::' : $class_name.'::').$match[1].'(', $out[$m]);
        }
        break;
      }
    }
  }
  $out[$n] = str_replace(array('/*<CLASS STATIC FUNC>*/', '/*</CLASS STATIC FUNC>*/'), array('', ''), $out[$n]);
  
  $out[$n] = preg_replace("#/\*<CLASS GLOBAL>\*/(.*?)/\*</CLASS GLOBAL>\*/#ms", '', $out[$n]);
  $out[$n] = preg_replace("#/\*<FUNC GLOBAL>\*/(.*?)/\*</FUNC GLOBAL>\*/#ms", '', $out[$n]);
  $out[$n] = preg_replace("#/\*<IF PROC>\*/(.*?)/\*</IF PROC>\*/#ms", '', $out[$n]);
  $out[$n] = str_replace(array('/*<IF CLASS>*//*', '*//*</IF CLASS>*/'), array('', ''), $out[$n]);
}

file_put_contents("vklang.php", $out[0]);
file_put_contents("init_parser.php", $out[1]);
